class Parser:
    '''
    This is the class all other parser should inherit from.
    '''
    
    tags = ()
    
    def __init__(self, settings=None, parsers=None):
        '''
        Makes sure that the settings and all other parsers are available. 
        '''
        
        if settings:
            assert type(settings)!=type({})
            self.settings = settings
        else:
            from symbide.setting import Settings
            self.settings = Settings()
            
        if type(parsers)==type({}):
            self.parsers = parsers
        else:
            import statement_parser, number_parser, plot_parser, string_parser
            s = self.settings
            p = {}
            p['statement'] = statement_parser.StatementParser(s,p)
            p['number'] = number_parser.NumberParser(s,p)
            p['string'] = string_parser.StringParser(s,p)
            p['plot'] = plot_parser.PlotParser(s,p)
            
            self.parsers = p
            
    def _updateTagsList(self, text):
        '''
        Stores the start end the end of all tags into tagsList.
        '''
        
        self.tagsList = []

        for a, b in self.tags:
            end = 0
            while True:
                start = end
                while start!=-1:
                    start = text.find(a, start)
                    if not self._insideTag(start):
                        break
                    start+=1
                    
                if start==-1:
                    break
                
                end = start
                while end!=-1:     
                    end = text.find(b, end)
                    if not self._insideTag(end):
                        break
                    end+=1
                    
                if end==-1:
                    break
                
                self.tagsList.append((start, end))
                
    def _insideTag(self, pos):
        '''
        Returns true if pos is inside a tag defined in tagsList.
        '''
        
        for start, end in self.tagsList:
            if start<pos<end:
                return True
        return False